package com.changgou.order.service.impl;

import com.changgou.order.dao.EvaluateMapper;
import com.changgou.order.pojo.Evaluate;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.EvaluateService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class EvaluateServiceImpl implements EvaluateService {
    @Autowired
    EvaluateMapper evaluateMapper;

    /**
     * 查询全部列表
     * @return
     */
    @Override
    public List<Evaluate> findAll() {
        return evaluateMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Evaluate findById(String id) {
        return evaluateMapper.selectByPrimaryKey(id);
    }

    /**
     * 增加
     * @param evaluate
     */
    @Override
    public void add(Evaluate evaluate) {
        evaluateMapper.insert(evaluate);
    }

    @Override
    public void update(Evaluate evaluate) {
        evaluateMapper.updateByPrimaryKey(evaluate);
    }

    @Override
    public void delete(String id) {
        evaluateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Evaluate> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return evaluateMapper.selectByExample(example);
    }

    @Override
    public Page<Evaluate> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        return (Page<Evaluate>)evaluateMapper.selectAll();
    }

    @Override
    public Page<Evaluate> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Evaluate>)evaluateMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(OrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // ID
            if(searchMap.get("id")!=null && !"".equals(searchMap.get("id"))){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            // SPU_ID
            if(searchMap.get("spuId")!=null && !"".equals(searchMap.get("spuId"))){
                criteria.andEqualTo("spuId",searchMap.get("spuId"));
            }
            // SKU_ID
            if(searchMap.get("skuId")!=null && !"".equals(searchMap.get("skuId"))){
                criteria.andEqualTo("skuId",searchMap.get("skuId"));
            }
            // 订单ID
            if(searchMap.get("orderId")!=null && !"".equals(searchMap.get("orderId"))){
                criteria.andEqualTo("orderId",searchMap.get("orderId"));
            }
            // 商品名称
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            // 商品名称
            if(searchMap.get("detailEval")!=null && !"".equals(searchMap.get("detailEval"))){
                criteria.andLike("detailEval","%"+searchMap.get("detailEval")+"%");
            }
            // 用户名
            if(searchMap.get("username")!=null && !"".equals(searchMap.get("username"))){
                criteria.andLike("username","%"+searchMap.get("username")+"%");
            }

            // 图片地址
            if(searchMap.get("orderItemId")!=null && !"".equals(searchMap.get("orderItemId"))){
                criteria.andLike("orderItemId","%"+searchMap.get("orderItemId")+"%");
            }
            // 商品评价
            if(searchMap.get("evaluate")!=null && !"".equals(searchMap.get("evaluate"))){
                criteria.andEqualTo("evaluate",searchMap.get("evaluate"));
            }
            // 是否退货
            if(searchMap.get("isReturn")!=null && !"".equals(searchMap.get("isReturn"))){
                criteria.andEqualTo("isReturn",searchMap.get("isReturn"));
            }


        }
        return example;
    }
}
